package leetcode8;

public class test43 {
    /**
     *114. 二叉树展开为链表
     * @param root
     */
    public void flatten(TreeNode root) {
        if(root == null)return;
        List<TreeNode> res = new ArrayList<>();
        dfs(root,res);
        TreeNode head = res.removeFirst();
        head.left = null;
        while(res.size() > 0){
            TreeNode temp = res.removeFirst();
            temp.left = null;
            head.right = temp;
            head = head.right;
        }
    }
    public void dfs(TreeNode root,List<TreeNode> res){
        if(root == null)
            return;
        res.add(root);
        dfs(root.left,res);
        dfs(root.right,res);
    }
}
